﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FF.Method;
using FF.Entity;

namespace FF
{
    public partial class FormUserManager : Form
    {
        public User currentUser;
        public FormUserManager()
        {
            //currentUser = cuser;
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //testAddUser();
            //testUpdateUser();
            //testDeleteUser();
            //DataSet dataset = SQLiteHelper.Query("select user_id,name,type,remark from ff_user");
            //dataGridView1.DataSource = dataset.Tables[0];
            //DataSet ds = SQLiteHelper.Query("select type,name from ff_user_type");
            //comboBoxUserType.DataSource = ds.Tables[0];
            //comboBoxUserType.DisplayMember = "name";
            //comboBoxUserType.ValueMember = "type"; 
            comboBoxUserType.Items.Add(new MyItem("用户姓名", "name"));
            comboBoxUserType.Items.Add(new MyItem("用户编号", "no"));
            comboBoxUserType.SelectedIndex = 0;
            //dataGridView1.Columns[0].Visible = false;
        }
        private void testAddUser()
        {
            User user = new User();
            //user.UserId =  new Random().Next(10000).ToString();
            user.Type = "A";
            user.UserName = "hexm";
            user.Password = "hexmsss";
            user.Remark = "test user add";
            UserMethod.userAdd(user);
        }
        private void testUpdateUser()
        {
            User user = new User();
            //user.UserId = "333";
            user.UserName = "roy";
            user.Password = "hexmsss";
            user.Remark = "test user update";
            UserMethod.userUpdate(user);

        }
        private void testDeleteUser()
        {

            UserMethod.userDelete("333");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataSet dataset = SQLiteHelper.Query("select user_id,a.name,b.name as type,a.remark from ff_user a,ff_user_type b where a.type = b.type");
            dataGridView1.DataSource = dataset.Tables[0];
            textBoxSearchKeyWord.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBoxSearchKeyWord.Text == "")
            {
                MessageBox.Show("请输入查找条件！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxSearchKeyWord.Focus();
                return;
            }
            string keyword = textBoxSearchKeyWord.Text.Trim();
            string searchType = ((MyItem)(comboBoxUserType.SelectedItem)).Value.ToString();
            string sql = "select user_id,a.name,b.name as type,a.remark from ff_user a,ff_user_type b where a.type = b.type and  ";
            //MessageBox.Show(((MyItem)(comboBoxUserType.SelectedItem)).Value.ToString());
            //MessageBox.Show(comboBoxUserType.SelectedIndex.ToString());
            if (searchType.Equals("no"))
            {
                sql += "a.user_id like '%" + keyword;
            }else{
                sql += "a.name  like '%" + keyword;
            }
            sql += "%'";
            //sql += "type=" + searchType;
            try
             {
	            DataSet ds = SQLiteHelper.Query(sql);
                if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
                {
                    MessageBox.Show("没有相关记录！", "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBoxSearchKeyWord.Focus();
                    return;
                }
                dataGridView1.DataSource = ds.Tables[0];
                return;
             }
             catch (System.Exception ex)
             {
                 MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return;
             }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show("dataGridView1_CellContentClick");
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show("dataGridView1_CellContentDoubleClick");
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show("dataGridView1_CellClick");
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
            //MessageBox.Show(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            //MessageBox.Show("dataGridView1_CellDoubleClick");
            string selectedUserId = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            User selectedUser = UserMethod.getUserById(selectedUserId);
            if(selectedUser != null)
            {
                FormUserModify form = new FormUserModify(currentUser);
                form.user = selectedUser;
                form.Owner = this;
                form.ShowDialog();
                if (textBoxSearchKeyWord.Text.Trim() == "")
                {
                    button2_Click(sender, e);
                }else
                {
                    button1_Click(sender, e);
                }
            }
        }
    }
}
